package com.xstech.library.app;

import android.app.ActivityManager;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.telephony.TelephonyManager;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.WindowManager;

import androidx.multidex.MultiDexApplication;

import com.xstech.library.utils.FileUtils;

import java.io.File;

public abstract class BaseApp extends MultiDexApplication {
    public static BaseApp getApp() {
        if (app == null) {
            Log.e("BaseApp", "BaseApp 的实例（例如：获取全局配置、初始化一些全局资源等）");
            // 可以抛出异常，或者返回一个默认的 app 实例来避免空指针
            throw new IllegalStateException("BaseApp is not initialized yet");
        }
        return app;
    }

    private static BaseApp app;


    public BaseApp() {
//        Log.d("BaseApp", "构造方法 用于初始化静态变量 app，并将当前的 BaseApp 实例赋给它");
        app = this;
    }

    public static BaseApp getInstance() {
        return app;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        this.app = this;
        onAppStart();
    }



    protected abstract void onAppStart();

    public String getDownloads() {
        File appFile = FileUtils.getAppDir();
        File downloads = new File(appFile, "Downloads");
        if (!downloads.exists()) {
            downloads.mkdirs();
        }
        return downloads.getAbsolutePath();
    }

    public String getScreen() {
//        获取屏幕的宽高，然后返回一个 "宽x高" 形式的字符串
        WindowManager manager = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics outMetrics = new DisplayMetrics();
        manager.getDefaultDisplay().getMetrics(outMetrics);
        int width = (int) outMetrics.widthPixels;
        int height = (int) outMetrics.heightPixels;
        return width + "x" + height;
    }

    public String getNetworkType() {
//        获取当前网络类型（WiFi、2G、3G、4G），然后返回对应的字符串，比如 "wifi"、"2g"、"3g"、"4g"。
        String strNetworkType = "";

        ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo networkInfo = cm.getActiveNetworkInfo();

        if (networkInfo != null && networkInfo.isConnected()) {
//            检查是否有网络连接，如果 networkInfo 为空或者没连接，说明 没有网络
            if (networkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
//                如果是 WiFi，直接返回 "wifi"。
                strNetworkType = "wifi";
            } else if (networkInfo.getType() == ConnectivityManager.TYPE_MOBILE) {
//                否则如果是移动数据（蜂窝网络），就要继续判断是 2G、3G 还是 4G。
                String _strSubTypeName = networkInfo.getSubtypeName();
                int networkType = networkInfo.getSubtype();

                switch (networkType) {
                    case TelephonyManager.NETWORK_TYPE_GPRS:
                    case TelephonyManager.NETWORK_TYPE_EDGE:
                    case TelephonyManager.NETWORK_TYPE_CDMA:
                    case TelephonyManager.NETWORK_TYPE_1xRTT:
                    case TelephonyManager.NETWORK_TYPE_IDEN: //api<8 : replace by 11
                        strNetworkType = "2g";
                        break;

                    case TelephonyManager.NETWORK_TYPE_UMTS:
                    case TelephonyManager.NETWORK_TYPE_EVDO_0:
                    case TelephonyManager.NETWORK_TYPE_EVDO_A:
                    case TelephonyManager.NETWORK_TYPE_HSDPA:
                    case TelephonyManager.NETWORK_TYPE_HSUPA:
                    case TelephonyManager.NETWORK_TYPE_HSPA:
                    case TelephonyManager.NETWORK_TYPE_EVDO_B: //api<9 : replace by 14
                    case TelephonyManager.NETWORK_TYPE_EHRPD:  //api<11 : replace by 12
                    case TelephonyManager.NETWORK_TYPE_HSPAP:  //api<13 : replace by 15
                        strNetworkType = "3g";
                        break;

                    case TelephonyManager.NETWORK_TYPE_LTE:    //api<11 : replace by 13
                        strNetworkType = "4g";
                        break;

                    default:
                        // http://baike.baidu.com/item/TD-SCDMA 中国移动 联通 电信 三种3G制式
                        if (_strSubTypeName.equalsIgnoreCase("TD-SCDMA") || _strSubTypeName.equalsIgnoreCase("WCDMA") || _strSubTypeName.equalsIgnoreCase("CDMA2000")) {
                            strNetworkType = "3g";
                        } else {
                            strNetworkType = _strSubTypeName;
                        }

                        break;
                }

            }
        }

        return strNetworkType;
    }

}
